package edu.must.uh.servlet;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import com.alibaba.fastjson.JSON;
import edu.must.uh.impl.BaseImpl;
import edu.must.uh.util.CookieUtil;
import edu.must.uh.util.LogHelper;
import edu.must.uh.util.PageBean;
import edu.must.uh.util.R;
import edu.must.uh.util.ServletUtils;
import edu.must.uh.util.Tools;
import edu.must.uh.util.ToolsOfString;


public class BaseServlet extends HttpServlet {
    private static final long serialVersionUID = 12197442526341123L;
    private static Logger logger = Logger.getLogger(BaseServlet.class);  
    protected PageBean pageBean = null;
    
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       
        request.setCharacterEncoding("UTF-8");  
        response.setCharacterEncoding("UTF-8"); 
        pageBean = null;
        long startTime=System.currentTimeMillis();    
        String md=ToolsOfString.NulltoEmpty(request.getParameter("method"));
        String cookiesUser = CookieUtil.getUid((HttpServletRequest)request, "LoginUser");
       //分頁參數處理
        String page=ToolsOfString.NulltoEmpty( request.getParameter("page")); 
        String limit=ToolsOfString.NulltoEmpty( request.getParameter("limit"));
        if(Tools.notEmpty(page) && Tools.notEmpty(limit) ) {
            pageBean = new PageBean();
            pageBean.setLimit(Integer.parseInt(limit) );
            pageBean.setPage(Integer.parseInt(page) );
        }
        
        Enumeration enu=request.getParameterNames(); 
        StringBuilder sBuilder = new StringBuilder();
        sBuilder.append(cookiesUser +"';");
        
        while(enu.hasMoreElements()){ 
          String paraName=(String)enu.nextElement();           
          sBuilder.append(paraName+": '"+request.getParameter(paraName) +"';");
        }       
        Object path=null;       
        Class<? extends BaseServlet> clazz = this.getClass();
        try { 
            sBuilder.append(request.getRequestURI() +":"+ md +";");
            System.out.println("currentUser: '"+sBuilder.toString());   
            logger.info(sBuilder.toString());
            Method method=clazz.getMethod(md, HttpServletRequest.class,HttpServletResponse.class);
            if(Tools.notEmpty(method)){                
                path=method.invoke(this, request,response);
            }       
            long endTime=System.currentTimeMillis();
            String costTime = endTime-startTime+"ms";
            logger.info("---service---"+costTime); 
            if(Tools.notEmpty(path)) {                              
                response.getWriter().write(JSON.toJSONString(path));
                response.getWriter().flush();
                response.getWriter().close();
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("uh",e);
        }
    }
}
